local skels = {}

local youge = fk.CreateSkill{
    name = "ym2__youge",
    tags = {Skill.Compulsory}
}
Fk:loadTranslationTable{
    ["ym2__youge"] = "游戈",
    [":ym2__youge"] = "锁定技，你使用伤害牌指定唯一目标后，游击：此牌无次数限制且结算结束后，令其攻击范围+1直到其回合结束，然后你可以用一张基本牌交换其区域内的两张牌。",
    ["#ym2__youge-give"] = "游戈：你可以用一张基本牌交换%dest区域内的两张牌",
    ["#ym2__youge-gain"] = "游戈：用%arg交换%dest区域内的两张牌",
    ["@ym2__youge"] = "游戈",
    ["@ym2__youge_can"] = "可游戈",
}

youge:addEffect(fk.TargetSpecified, {
    anim_type = "offensive",
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(youge.name) and data.card.is_damage_card
        and data:isOnlyTarget(data.to) and not data.to.dead and player:inMyAttackRange(data.to) and not data.to:inMyAttackRange(player)
    end,
    on_use = function (self, event, target, player, data)
        if not data.use.extraUse then
            player:addCardUseHistory(data.card.trueName, -1)
            data.use.extraUse = true
        end
        data.extra_data = data.extra_data or {}
        data.extra_data.ym2__youge = data.extra_data.ym2__youge or {}
        table.insert(data.extra_data.ym2__youge, data.to.id)
    end,
})

youge:addEffect(fk.CardUseFinished, {
    anim_type = "control",
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        return data.extra_data and data.extra_data.ym2__youge and target == player
    end,
    on_cost = Util.TrueFunc,
    on_use = function (self, event, target, player, data)
        local room = player.room
        for _, cpID in ipairs(data.extra_data.ym2__youge) do
            local cp = room:getPlayerById(cpID)
            room:addPlayerMark(cp, "@ym2__youge", 1)
            if #cp:getCardIds("hej") > 1 then
                local cards1 = room:askToCards(player, {
                    min_num = 1,
                    max_num = 1,
                    include_equip = false,
                    skill_name = youge.name,
                    pattern = ".|.|.|.|.|basic",
                    prompt = "#ym2__youge-give::"..cp.id,
                    cancelable = true,
                })
                if #cards1 > 0 then
                    local cards2 = cp:getCardIds("hej")
                    if #cards2 > 2 then
                        cards2 = room:askToChooseCards(player, {
                            target = cp,
                            flag = "hej",
                            max = 2,
                            min = 2,
                            skill_name = youge.name,
                            prompt = "#ym2__youge-gain::"..cp.id..":"..Fk:getCardById(cards1[1]):toLogString(),
                        })
                    end
                    room:swapCards(player, {
                        {player, cards1},
                        {cp, cards2},
                    }, youge.name)
                end
            end
        end
    end,
})
youge:addEffect(fk.TurnEnd, {
    late_refresh = true,
    can_refresh = function (self, event, target, player, data)
        return player:getMark("@ym2__youge") > 0
    end,
    on_refresh = function (self, event, target, player, data)
        player.room:setPlayerMark(player, "@ym2__youge", 0)
    end,
})
youge:addEffect("atkrange", {
    correct_func = function (self, from, to)
        return from:getMark("@ym2__youge")
    end,
})
table.insert(skels, youge)

local slash_skill = fk.CreateSkill{
    name = "ym2z_machao__slash_skill",
}
slash_skill:addEffect("cardskill", {
    prompt = function(self, player, selected_cards)
        local slash = Fk:cloneCard("slash")
        slash:addSubcards(selected_cards)
        local max_num = self:getMaxTargetNum(player, slash) -- halberd
        if max_num > 1 then
        local num = #table.filter(Fk:currentRoom().alive_players, function (p)
            return p ~= player and not player:isProhibited(p, slash)
        end)
        max_num = math.min(num, max_num)
        end
        return max_num > 1 and "#slash_skill_multi:::" .. max_num or "#slash_skill"
    end,
    max_phase_use_time = 1,
    target_num = 1,
    can_use = function(self, player, card, extra_data)
        if player:prohibitUse(card) then return end
        return (extra_data and extra_data.bypass_times) or player.phase ~= Player.Play or
        table.find(Fk:currentRoom().alive_players, function(p)
            return self:targetFilter(player, p, {}, {}, card, extra_data)
        end) ~= nil
    end,
    mod_target_filter = function(self, player, to_select, selected, card, extra_data)
        return to_select ~= player and
        not (not (extra_data and extra_data.bypass_distances) and not self:withinDistanceLimit(player, true, card, to_select))
    end,
    target_filter = function(self, player, to_select, selected, _, card, extra_data)
        if not Util.CardTargetFilter(self, player, to_select, selected, _, card, extra_data) then return end
        return self:modTargetFilter(player, to_select, selected, card, extra_data) and
        (
            #selected > 0 or
            player.phase ~= Player.Play or
            (extra_data and extra_data.bypass_times) or
            self:withinTimesLimit(player, Player.HistoryPhase, card, "slash", to_select)
        )
    end,
    target_tip = function (self, player, to_select, selected, selected_cards, card, selectable, extra_data)
        if #selected == 0 and player:hasSkill(youge.name) then
            if self:targetFilter(player, to_select, selected, selected_cards, card, extra_data) then
                if card.skillName == "ym2__fengxi" then
                    --不丢锋袭，也不丢马术时
                    if player:inMyAttackRange(to_select, -1) and not to_select:inMyAttackRange(player, -1) then
                        return "@ym2__youge_can"
                    end
                    --丢锋袭时
                    if player:inMyAttackRange(to_select) and not to_select:inMyAttackRange(player) then
                        return "@ym2__youge_can"
                    end
                    --不丢锋袭，丢马术时
                    if player:inMyAttackRange(to_select, -2) and not to_select:inMyAttackRange(player, -1) then
                        return "@ym2__youge_can"
                    end
                else
                    --不给或给自己拿马术
                    if (player:inMyAttackRange(to_select) or player:inMyAttackRange(to_select, 1)) and not to_select:inMyAttackRange(player) then
                        return "@ym2__youge_can"
                    end
                end
            end
        end
    end,
    on_effect = function(self, room, effect)
        if not effect.to.dead then
        room:damage({
            from = effect.from,
            to = effect.to,
            card = effect.card,
            damage = 1,
            damageType = fk.NormalDamage,
            skillName = "slash_skill"
        })
        end
    end,
})
table.insert(skels, slash_skill)

return skels